Comparing two images files from the two folder one by one using python language.

by: selva, 8 years ago


import cv2
import numpy as np
#import os

file1= "C:Program Files (x86)Python35-32file1"
file2="C:Program Files (x86)Python35-32file2"
for f1 in file1:
    image1 = cv2.imread(f1)
for f2 in file2:
    image2 = cv2.imread(f2)
difference = cv2.subtract(image1, image2)
result = not np.any(difference) #if difference is all zeros it will return False

if result is True:
     print("The images are the same")
else:
     cv2.imwrite("result.jpg", difference)
     print ("the images are different")

but the above code seems to not working as expected. I know the for loops is not correct. I am new to python. Can you please let me what i am doing wrong here please?



You must be logged in to post. Please login or register an account.



Notice how the code you posted here removed your backslashes? in file1 and file2? The backslash is an "escape character" in python. Windows uses them in their directories, and this can cause some trouble.

Thus, either use a double backslash for every backslash in your path (effectively escaping the "value" of the backslash), or use forward slashes. /

-Harrison 8 years ago
Last edited 8 years ago

You must be logged in to post. Please login or register an account.


Thanks, Harrison. I will try this steps and let you know the results.

-selva 8 years ago

You must be logged in to post. Please login or register an account.

heres a working compare and copy script, that copies only similar file names in two folders to a new folder. cheers.

import os, shutil
f1 = '/Users/j1new/Desktop/temp/'
d1 = os.listdir(f1)
f2 = '/Users/j1new/Desktop/temp2/
d2 = os.listdir(f2)
fnew = '/Users/j1new/Desktop/fnew/'

def comparecopy():
    files = list(set(d1) & set(d2))
    #print(files)
    for f in files:
        shutil.copy(os.path.join(f1, f), fnew)
    print('Compare & copy successful')

if __name__ == '__main__':
    comparecopy()

input()


-kingfitz 8 years ago
Last edited 8 years ago

You must be logged in to post. Please login or register an account.


Hi King Fitz, Thanks for your response and help. I will try this.

-selva 8 years ago

You must be logged in to post. Please login or register an account.

I have tried as below but it doesn't work. can some let me know what i am doing wrong here.
import cv2
import numpy as np
import os

file1= os.listdir("C:/Program Files (x86)/Python35-32/file1")
file2=os.listdir("C:/Program Files (x86)/Python35-32/file2")

for f1 in file1:
    if f1.endswith(".png"):
       image1 = cv2.imread(f1)
for f2 in file2:
    if f2.endswith(".png"):
       image2 = cv2.imread(f2)
       difference = cv2.subtract(image1, image2)
       result = not np.any(difference) #if difference is all zeros it will return False

if result is True:
     print("The images are the same")
else:
     cv2.imwrite("result.jpg", difference)
     print ("the images are different")

-selva 8 years ago
Last edited 8 years ago

You must be logged in to post. Please login or register an account.